<?php
if(!class_exists('EventManager_Resource_Event_Item')){
	require_once dirname(__FILE__) . '/resources/Event/Item.php';
}
if (!class_exists('EventManager_Resource_Adres')) {
	require_once dirname(__FILE__) . '/resources/Adres.php';
}
/**
 *
 * @author jigal
 *
 */
class EventManager_Model_Event extends SF_Model_Acl_Abstract{


	
	
	
	public function getEventById($id)
	{
		return $this->getResource('Event')->getEventById($id);
	}
	public function getShiftsByEvent()
	{
		
	}
	
	public function getSubscriptionsByEvent()
	{
		
	}
	
	/**
	 *
	 * @param $startDate
	 * @param $endDate
	 * @return unknown_type
	 */
	public function getAllEvents($startDate = null, $endDate = null,$page)
	{
		return $this->getResource('Event')->getAllEvents($startDate,$endDate,$page);
	}
	
	/**
	 * 
	 * @param $post
	 * @return unknown_type
	 */
	public function add($post){
		

		//var_dump($post);exit;
		$form = $this->getForm('eventNew');
			return $this->_save(
				$form,
				$post,
				array()
		);
		
	}
	
	protected function _save(Zend_Form $form, array $info, $defaults= array()){
		if(!$form->isValid($info)){
			return false;
		}
		$data = $form->getValues();
		
		
		foreach ($defaults as $col => $value){
			$data['col'] = $value;
		}
		$data['sts_id'] = 1;
		
		
		$evt = array_key_exists('evt_id') ? $this->getResource('event')->getEventById($data['evt_id']):null;
		return $this->getResource('Event')->saveRow($data,$evt);
	}
	
	/**
	 *
	 * @return unknown_type
	 */
	public function getResourceId(){
		return 'event';
	}
	/**
	 *
	 * @return unknown_type
	 */
	public function getAcl(){
		if(null === $this->_acl){
			$this->setAcl(new EventManager_Model_Acl_EventManager());
		}
		return $this->_acl;

	}

	public function setAcl(SF_Acl_Interface $acl){
		if(!$acl->has($this->getResourceId())){
			$acl->add($this)
			->allow('User', $this,array('getAllEvents'))
			->allow('Admin',$this);
		}
		return $this->_acl = $acl;

	}

}